<?php
namespace app\admin2\controller;

use app\common\model\OnlineAsk as OnlineAskModel;
use app\common\model\OnlineMessage as OnlineMessageModel;
use api\controller\Upload;
use app\common\controller\AdminBaseo;

class OnlineAsk extends AdminBaseo
{
    protected $online_ask_model;
    protected $online_message_model;

    protected function _initialize(){
        parent::_initialize();
        $this->online_ask_model = new OnlineAskModel;
        $this->online_message_model = new OnlineMessageModel;
    }

    /**
     * 查看信息列表详情
     */
    public function showMessageList($id){
        $list = $this->online_message_model->where('mid',$id)->select();
        if($list){
            return json(['status' => 1 , 'msg' => 'ok' , 'list' => $list]);
        }else{
            return json(['status' => -1 , 'error' => 'get message failed']);
        }
    }

    /**
     * 查看在线回复列表
     */
    public function getOnlineAskList(){
        $list = $this->online_ask_model->showList();
        if($list){
            return json(['status' => 1 , 'msg' => 'ok' , 'list' => $list]);
        }else{
            return json(['status' => -1 , 'error' => 'get message failed']);
        }
    }

    /**
     * 查看在线回复内容
     */
    public function getOnlineAskDetail($id){
        $list = $this->online_ask_model->showOne($id);
        $list['speech'] = html_entity_decode($list['speech']);
        if($list){
            return json(['status' => 1 , 'msg' => 'ok' , 'list' => $list]);
        }else{
            return json(['status' => -1 , 'error' => 'get message failed']);
        }
    }


    /**
     * 添加在线回复内容
     */
    public function save(){
        if($this->request->isPost()){
            $data = $this->request->post();
            // if(isset($data['id'])) unset($data['id']);
            $result = $this->online_ask_model->allowField(true)->save($data);
            if($result){
                return json(['status' => 1 , 'msg' => 'ok']);
            }else{
                return json(['status' => -1 , 'error' => 'save error']);
            }
        }
    }


    /**
     * 修改在线回复内容
     */
    public function update($id){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = $this->online_ask_model->allowField(true)->save($data,['id' => $id]);
            if($result){
                return json(['status' => 1 , 'msg' => 'ok']);
            }else{
                return json(['status' => -1 , 'error' => 'save error']);
            }
        }
    }
}